package com.dwy2002.order.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.dwy2002.order.mapper.OrderCourseMapper;
import com.dwy2002.order.service.IOrderCourseService;
import com.dwy2002.pojo.dto.order.OrderCourseDTO;
import com.dwy2002.pojo.po.courses.Courses;
import com.dwy2002.pojo.po.order.OrderCourse;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.messaging.handler.annotation.Header;
import org.springframework.stereotype.Service;

import static com.dwy2002.common.utils.Utils.isEmptyOrNull;
import static com.dwy2002.common.utils.Utils.validateIdNotEmpty;
import static com.dwy2002.enums.rabbit.CourseRabbitConstants.*;

/**
 * @author 杜伟毅
 * @version 1.0
 * @since 2025/03/06
 */
@Service
@RequiredArgsConstructor
public class OrderCourseServiceImpl extends ServiceImpl<OrderCourseMapper, OrderCourse> implements IOrderCourseService {
    private final RabbitTemplate rabbitTemplate;

    /**
     * 保存订单课程类
     *
     * @param orderId 订单ID
     * @param dto     订单课程DTO
     * @author 杜伟毅
     * @since 2025/3/9 0009 19:36
     */
    @Override
    public void saveOrderCourse(String orderId, OrderCourseDTO dto) {
        if (dto == null || isEmptyOrNull(orderId)) return;
        OrderCourse orderCourse = BeanUtil.copyProperties(dto, OrderCourse.class);
        orderCourse.setOrderId(orderId);
        //暂存关系
        save(orderCourse);
        // 发送消息到 RabbitMQ，请求生成 enrollmentId
        rabbitTemplate.convertAndSend(
                ENROLLMENT_REQUEST_EXCHANGE,
                ENROLLMENT_REQUEST_ROUTING_KEY,
                dto.getCourseId(),
                message -> {
                    // 关联订单ID，用于后续处理
                    message.getMessageProperties().setHeader("orderId", orderId);
                    return message;
                }
        );
    }

    /**
     * 获取课程信息
     *
     * @param orderId 订单ID
     * @return 返回课程信息
     * @author 杜伟毅
     * @since 2025/3/12 0012 20:08
     */
    @Override
    public Courses getCoursesByOrderId(String orderId) {
        validateIdNotEmpty(orderId, "getCoursesByOrderId");
        LambdaQueryWrapper<OrderCourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderCourse::getOrderId, orderId);
        return Db.getById(getOne(lqw).getCourseId(), Courses.class);
    }

    /**
     * 根据订单id删除查询信息
     *
     * @param orderId 订单ID
     * @author 杜伟毅
     * @since 2025/3/12 0012 16:13
     */
    @Override
    public void deleteOrderCourse(String orderId) {
        validateIdNotEmpty(orderId, "deleteOrderCourse");
        LambdaQueryWrapper<OrderCourse> lqw = new LambdaQueryWrapper<>();
        lqw.eq(OrderCourse::getOrderId, orderId);
        remove(lqw);
    }

    //辅助方法-----------------------------------------------------------

    /**
     * 接收查出来的报名id，写入课程订单表
     *
     * @param enrollmentId 报名id
     * @param orderId      订单id
     * @author 杜伟毅
     * @since 2025/3/22 0022 22:07
     */
    @RabbitListener(queues = ENROLLMENT_RESPONSE_QUEUE)
    public void handleEnrollmentResponse(String enrollmentId, @Header("orderId") String orderId) {
        // 根据 orderId 找到订单，设置 enrollmentId
        LambdaUpdateWrapper<OrderCourse> luw = new LambdaUpdateWrapper<>();
        luw.eq(OrderCourse::getOrderId, orderId)
                .set(OrderCourse::getEnrollmentId, enrollmentId);
        update(luw);
    }
}
